package com.buddies.lottery.base.list;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.buddies.lottery.App;
import com.buddies.lottery.R;
import com.buddies.lottery.base.BaseActivity;
import com.buddies.lottery.data.result.PageResult;
import com.buddies.lottery.widget.OnTabClickListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.r0adkll.slidr.Slidr;

import java.util.List;


public abstract class BaseListActivity<D, P extends IBaseListPersenter> extends BaseActivity implements OnTabClickListener {

    protected SwipeRefreshLayout mSwipeRefreshLayout;
    protected GridLayoutManager mLayoutManager;
    protected RecyclerView mRecyclerView;
    protected int mColumn = 1;
    protected BaseQuickAdapter mAdapter;

    protected int page = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        Slidr.attach(this, App.getSlidrConfig());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_list;
    }

    @Override
    protected void initView() {
        setUp();

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresher);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                forceRefresh();
            }
        });

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    }

    @Override
    protected void initData() {
        setTitle(getHeaderTitle());
        setOnTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onTabClick();
            }
        });

        mAdapter = createAdapter();
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                onLoadMore();
            }
        }, mRecyclerView);
        mAdapter.setEmptyView(R.layout.view_empty, (ViewGroup) findViewById(R.id.container));

        mLayoutManager = new GridLayoutManager(this, mColumn, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mAdapter);

        forceRefresh();
    }

    protected abstract String getHeaderTitle();

    protected abstract BaseQuickAdapter createAdapter();

    protected void forceRefresh() {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(true);
                    mRecyclerView.smoothScrollToPosition(0);
                    page = 1;
                    onLoadData(page);
                }
            });
        }
    }

    protected abstract void onLoadData(int page);

    public void onLoadMore() {
        page ++;
        onLoadData(page);
    }

    public void onDataLoaded(PageResult<D> response) {
        mSwipeRefreshLayout.setRefreshing(false);
        List list = response.list;
        if (response.page <= 1) {
            mAdapter.setNewData(list);
        } else {
            mAdapter.addData(list);
        }
        if (list != null && !list.isEmpty()) {
            mAdapter.loadMoreComplete();
        } else {
            mAdapter.loadMoreEnd();
        }
    }

    @Override
    public void onError(Throwable e) {
        super.onError(e);
        mSwipeRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreFail();
    }

    @Override
    public void onTabClick() {
        if ((mLayoutManager != null && mLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) || mAdapter.getItemCount() == 0) {// 第一页
            forceRefresh();
        } else if (mRecyclerView != null && mLayoutManager != null) {
            int currentPosition = mLayoutManager.findFirstCompletelyVisibleItemPosition();
            if (currentPosition > 12) {
                mRecyclerView.scrollToPosition(12);
            }
            mRecyclerView.smoothScrollToPosition(0);
        }
    }
}
